using System;
using System.Linq;
using CommandSystem;
using CustomCharacter.VoiceChat;
using Exiled.API.Features;
using RgCore.API;
using VoiceChat;

namespace CustomCharacter.Commands
{
    public class VoiceChannelCommand : ICommand
    {
        public string Command => "voicechannel";

        public string[] Aliases => new string[] { "vc" };

        public string Description => "修改语音频道";

        public bool Execute(ArraySegment<string> arguments, ICommandSender sender, out string response)
        {
            if (arguments.Count < 3)
            {
                response = "用法：voicechannel/vc <玩家ID> primary <VoiceChatChannel/clear>\n" +
                           "voicechannel/vc <玩家ID> <send/receive> <add/remove/clear> [VoiceChatChannel]";
                return false;
            }
            if (!int.TryParse(arguments.At(0), out int id))
            {
                response = "请输入一个有效的玩家ID";
                return false;
            }
            Player player = Player.Get(id);
            if (player == null)
            {
                response = "未找到指定玩家";
                return false;
            }
            switch (arguments.At(1).ToLower())
            {
                case "primary":
                    if (arguments.At(2).ToLower() == "clear")
                    {
                        player.VC_ResetPrimaryChannel();
                        response = "已清除主要频道设定";
                        return true;
                    }
                    if (!Enum.TryParse(arguments.At(2), out  VoiceChatChannel voiceChatChannel))
                    {
                        response = "无效的频道";
                        return false;
                    }
                    player.VC_SetPrimaryChannel(voiceChatChannel);
                    response = "已将主要频道设定为：" + voiceChatChannel.GetChannel();
                    return true;
                case "send":
                case "receive":
                {
                    switch (arguments.At(2).ToLower())
                    {
                        case "add":
                        case "remove":
                            if (arguments.Count < 4)
                            {
                                response = "必须提供一个频道！";
                                return false;
                            }
                            bool flag1 = Enum.TryParse(arguments.At(3), out VoiceChatChannel vc);
                            CustomChannel cc = VoiceChatManager.CustomChannels.FirstOrDefault(x => x.Id == arguments.At(3));
                            bool flag2 = cc != null;
                            if (!flag1 && !flag2)
                            {
                                response = "无效的频道";
                                return false;
                            }
                            if (arguments.At(1).ToLower() == "send")
                            {
                                if (arguments.At(2).ToLower() == "add")
                                {
                                    if (flag1)
                                    {
                                        player.VC_AddSender(vc, false);
                                        response = "已添加抄送频道：" + vc.GetChannel();
                                    }
                                    else
                                    {
                                        player.VC_AddSender(cc);
                                        response = "已添加抄送频道：" + cc.Name + $" [Override: {cc.PrimaryCovery}]";
                                    }
                                    return true;
                                }
                                if (arguments.At(2).ToLower() == "remove")
                                {
                                    if (flag1)
                                    {
                                        player.VC_RemoveSender(vc);
                                        response = "已移除抄送频道：" + vc.GetChannel();
                                    }
                                    else
                                    {
                                        player.VC_RemoveSender(cc);
                                        response = "已移除抄送频道：" + cc.Name;
                                    }
                                    return true;
                                }
                            }
                            if (arguments.At(1).ToLower() == "receive")
                            {
                                if (arguments.At(2).ToLower() == "add")
                                {
                                    if (flag1)
                                    {
                                        player.VC_AddListener(vc);
                                        response = "已添加监听频道：" + vc.GetChannel();
                                    }
                                    else
                                    {
                                        player.VC_AddListener(cc);
                                        response = "已添加监听频道：" + cc.Name;
                                    }
                                    return true;
                                }
                                if (arguments.At(2).ToLower() == "remove")
                                {
                                    if (flag1)
                                    {
                                        player.VC_RemoveListener(vc);
                                        response = "已移除监听频道：" + vc.GetChannel();
                                    }
                                    else
                                    {
                                        player.VC_RemoveListener(cc);
                                        response = "已移除监听频道：" + cc.Name;
                                    }
                                    return true;
                                }
                            }
                            response = "遇到了未知的错误";
                            return false;
                        case "clear":
                            if (arguments.At(1).ToLower() == "send")
                            {
                                player.VC_ClearSender();
                                response = "已清除抄送频道！";
                                return true;
                            }
                            if (arguments.At(1).ToLower() == "receive")
                            {
                                player.VC_ClearListener();
                                response = "已清除侦听频道！";
                                return true;
                            }
                            response = "遇到了未知的错误";
                            return false;
                        default:
                            response = "用法：voicechannel/vc <玩家ID> primary <VoiceChatChannel/clear>\n" +
                                       "voicechannel/vc <玩家ID> <send/receive> <add/remove/clear> [VoiceChatChannel]";
                            return false;
                    }
                }
                default:
                    response = "用法：voicechannel/vc <玩家ID> primary <VoiceChatChannel/clear>\n" +
                               "voicechannel/vc <玩家ID> <send/receive> <add/remove/clear> [VoiceChatChannel]";
                    return false;
            }
        }
    }
}